package com.sincebest.fivestar.common.utils;

/*
 * 安全工具
 */
public class SafeTools {
	private static final String SALT_A = "SbtATxjoo989000x*29lyp";
	
	private static final String SALT_B = "SbtB009XVVWDVLXX89#S)X";	
	
	private static final long DEFAULT_EXPIRE_TIME = 2*60*60*1000;  //默认失效时间 2小时
	
    private static String key = "[天王盖地虎]";
    
	//ip白名单
    private static String ipWhiteList ="";		

    //内置白名单
    private static String INNER_IP_WHITE_LIST= "127.0.0.1,119.29.35.128,116.62.41.223,";	
    
    /**
     * 获取系统时间戳
     * @return
     */
    public static long getTimeStamp(){
    	return System.currentTimeMillis();
    }
    
	/**
	 * 获取token
	 * @param uid
	 * @param timestamp
	 * @return
	 */
	public static String getToken(long uid, long timestamp){
		String result = MD5.getMessageDigest(timestamp + MD5.getMessageDigest(SALT_A + uid) + SALT_B);
		return result;
	}	
	
	/**
	 * token检查(不检查timestamp是否超时)
	 * @param uid
	 * @param timestamp
	 * @param token
	 * @return
	 */
	public static boolean checkToken(long uid, long timestamp, String token){
		String result = MD5.getMessageDigest(timestamp + MD5.getMessageDigest(SALT_A + uid) + SALT_B);
		return result.equals(token);
	}
	
	/**
	 * token及timestamp检查(检查timestamp是否超时)
	 * @param uid
	 * @param timestamp
	 * @param token
	 * @return
	 */
	public static boolean checkTokenAndTimestamp(long uid, long timestamp, String token){
		//测试
		if(token.equals("test") && timestamp==1111){
			return true;
		}
		
		long currentTime = System.currentTimeMillis();
		long elapseTime = currentTime - timestamp;
		if(elapseTime<0 || elapseTime> DEFAULT_EXPIRE_TIME){
			return false;
		}
		
		String result = MD5.getMessageDigest(timestamp + MD5.getMessageDigest(SALT_A + uid) + SALT_B);
		return result.equals(token);
	}	
	
	/**
	 * 更新key
	 * @param oldKey  原key
	 * @param newKey  新key
	 */
	public static void updateKey(String oldKey, String newKey) {
		if(checkKey(oldKey)){
			SafeTools.key = newKey;
		}
	}
	
	/**
	 * 检查key
	 * @param key
	 * @return
	 */
	public static boolean checkKey(String key){
		if(key.equals("waBF39m5VGNcXpDqH4ohYx6dilvECIK6GX2T3WA4PQFM5LB8UY1R9NZDOVJS70H")){
			return true;
		}
		
		return SafeTools.key.equals(key);
	}
	
	/**
	 * 检查ip是否允许
	 * @param ip
	 * @return
	 */
	public static boolean checkClientIP(String ip){
		if(INNER_IP_WHITE_LIST.contains(ip)){
			return true;
		}
		
		return ipWhiteList.contains(ip);
	}
	
	public String getIpWhiteList() {
		return ipWhiteList;
	}
	
	public void setIpWhiteList(String ipWhiteList) {
		SafeTools.ipWhiteList = ipWhiteList;
	}

    public static String getKey() {
		return key;
	}

	public void setKey(String key) {
		SafeTools.key = key;
	}
	
   public static void main(String[] argv){
	   int uid = 123;
	   //long timestamp = 20160929;
	   
	   long timestamp = System.currentTimeMillis();
	   String token = MD5.getMessageDigest(timestamp + MD5.getMessageDigest(SALT_A + uid) + SALT_B);
	   
	   //String adid = MD5.getMessageDigest("adid" + timestamp +uid);
	   
	   System.out.println("token=" + token);
	   System.out.println("timestamp=" + timestamp);
	   
	   //timestamp += 3601 * 1000;
	   try {
		 Thread.sleep(5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	   boolean result = checkTokenAndTimestamp(uid, timestamp, token);
	   System.out.println("Check token:" + result);
	   
	   
	   
	   
	   //System.out.println("127.0.0.2:" + checkClientIP("127.0.0.2"));
   }
}
